<template>
	<view id="message">
		<Header txt="信息中心" v-if="baidu"></Header>
		<HeaderB txt="信息中心" v-if="!baidu"></HeaderB>
		<view class="top-tit">
			购买订单,认证信息等会通过短信或站内信发送给你
		</view>
		<view class="msg" v-for="(item, index) in message" :key="index" @click="goGoodsPage(item.msgId)">
			<view class="time">
				{{item.pushTime}}
			</view>
			<view class="title">
				<text v-if="item.status==0" class="cirtle_red"></text>
				{{item.title}}
			</view>
			<view class="content">
				{{item.content}}...
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				message:[],
				pageNum:1,
				baidu:true
			}
		},
		onLoad() {
			this.msgList()
		},
		onShow() {
			this.msgList()
			// #ifdef MP-BAIDU
			this.baidu=false
			// #endif
		},
		onReachBottom(){
			this.msgList()
		},
		methods: {
			parseTime(time, cFormat) {
			     if (arguments.length === 0) {
			       return null;
			     }
			     const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
			     let date;
			     if (typeof time === "object") {
			       date = time;
			     } else {
			       if (("" + time).length === 10) time = parseInt(time) * 1000;
			       date = new Date(time);
			     }
			     const formatObj = {
			       y: date.getFullYear(),
			       m: date.getMonth() + 1,
			       d: date.getDate(),
			       h: date.getHours(),
			       i: date.getMinutes(),
			       s: date.getSeconds(),
			       a: date.getDay()
			     };
			     const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
			       let value = formatObj[key];
			       if (key === "a")
			         return ["一", "二", "三", "四", "五", "六", "日"][value - 1];
			       if (result.length > 0 && value < 10) {
			         value = "0" + value;
			       }
			       return value || 0;
			     });
			     return time_str;
			   },
			msgList(){
				let that = this;
				this.$store
				  .dispatch("MessageList",{pageNum:this.pageNum})
				  .then((response) => {
					if(response.data.code==0){
						this.pageNum++
						let tar =response.data.obj.message == undefined ? [] : response.data.obj.message;
						tar.forEach((item) => {
						            item.pushTime = that.parseTime(item.pushTime);
						          });
						that.message=[...that.message, ...tar];					
					}
				  })
				  .catch(() => {});
			},
			goGoodsPage(id){
				let tar=this.message
				tar.forEach((item) => {
				          if(item.msgId==id){
							  item.status=1
						  }
				    });
				uni.navigateTo({
					url:'/pages/user/msgDel?id='+decodeURIComponent(`${id}`)
				})
			}
		}
	}
</script>

<style lang="less">
	#message{
		overflow: hidden;
		background:  #F5F5F5;
		.top-tit{
			background: #fff7dd;
			color: #999999;
			font-size: 24rpx;
			text-align: center;
			padding-top: 4rpx;
		}
		.msg{
			margin: 20rpx;
			padding: 20rpx;
			background: #ffffff;
			border-radius: 10rpx;
			.time{
				font-size: 28rpx;
				color: #888888;
			}
			.title{
				font-size: 34rpx;
				color: #222222;
				font-weight: bold;
				margin: 20rpx 0;
				position: relative;
				.cirtle_red{
					display: inline-block;
					width: 12rpx;
					height: 12rpx;
					background: #f85651;
					border-radius: 50%;
					position: relative;
					top:-6rpx;
					border-radius: 50%;
				}
			}
			.content{
				font-size: 30rpx;
				color: #757c98;
				line-height: 48rpx;
			}
		}
	}
</style>
